// packageF/silverPointPay/addSilverBank/addSilverBank.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      name: "",
      card: "",
      bank: "",
      card_type: "",
      identity: "",
      mobile: "",
      cvv2: "",
      valid_day: ""
    },
    step: 1,
    // 所属银行弹窗
    bankShow: false,
    // 信用卡类型弹窗
    cardShow: false,
    radio: "",
    radioCard: "",
    bankArr: ["中信银行", "上海银行", "光大银行", "民生银行", "北京银行", "平安银行", "交通银行", "招行银行", "招行银行", "广发银行", "华夏银行", "建设银行", "农业银行", "浦发银行", "邮储银行"],
    bankCard: [{
        name: "储蓄卡",
        id: 1
      },
      {
        name: "信用卡",
        id: 2
      }
    ],
    // 有效期选择
    timeShow: false,
    maxDate: new Date().getTime(),
    currentDate: new Date().getTime(),

    // 第二步，验证码
    // 图形验证码
    imgcode: "",
    // 是否开启图形验证码
    imgcodeShow: false,
    sms: "",
    smsMobile: "",
    btnText: "获取验证码",
    timer: null,
    btnTag: false,
    order_pay_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '添加银行卡',
    })
    if (options.order_pay_id) {
      this.setData({
        order_pay_id: options.order_pay_id
      })
    }
    if (options.sn) {
      this.setData({
        sn: options.sn
      })
    }
    if (options.money) {
      this.setData({
        money: options.money
      })
    }
  },
  valInp(e) {
    let type = e.detail.type;
    let val = e.detail.value;
    this.setData({
      ["form." + type]: val,
    })
  },
  childParams(e) {
    let type = e.detail;
    if (type == "valid_day") {
      // 有效期
      this.setData({
        timeShow: true
      })
    } else if (type == "card_type") {
      // 卡类型
      this.setData({
        cardShow: true
      })
    } else if (type == "bank") {
      // 所属银行
      this.setData({
        bankShow: true
      })
    }
  },
  cardShowClose() {
    this.setData({
      cardShow: false
    })
  },
  bankShowClose() {
    this.setData({
      bankShow: false
    })
  },
  bankRadio(e) {
    let val = e.detail;
    this.setData({
      radio: val
    })
  },
  bankCardRadio(e) {
    this.setData({
      radioCard: e.detail
    })
  },
  onClickRight() {
    this.setData({
      "form.bank": this.data.radio,
      bankShow: false
    })
  },
  onClickRight_timeShow(e) {
    let time = this.timestampToTime(e.detail)
    this.setData({
      'form.valid_day': time,
      timeShow: false
    })
  },
  onClickRight_cardShow() {
    //银行卡类型
    this.setData({
      'form.card_type': this.data.radioCard == "储蓄卡" ? 1 : 2,
      cardShow: false
    })
  },
  timeShowClose() {
    this.setData({
      timeShow: false
    })
  },
  // 时间戳转日期
  timestampToTime: function (timestamp) {
    if (!Number(timestamp) || timestamp == "") {
      return "";
    }
    let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear();
    let M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;

    return M + Y;
  },
  next() {
    console.log(this.data.form, 'dddddddd')
    if (this.data.step == 1) {
      if (this.verify()) {
        // 第二部 验证图形码,手机验证码
        this.setData({
          step: 2
        })
        this.getimgStatus();
      }
    }
  },
  smsInp(e) {
    this.setData({
      sms: e.detail
    })
    console.log(this.data.sms)
  },
  smsMobileInp(e) {
    this.setData({
      smsMobile: e.detail
    })
    console.log(this.data.smsMobile)
  },
  verify() {
    if (app._isTextEmpty(this.data.form.name)) {
      wx.showModal({
        content: '请输入持卡人姓名',
        showCancel: false
      })
      return false;
    }
    if (app._isTextEmpty(this.data.form.card)) {
      wx.showModal({
        content: '请输入卡号',
        showCancel: false
      })
      return false;
    }
    if (app._isTextEmpty(this.data.form.bank)) {
      wx.showModal({
        content: '请选择所属银行',
        showCancel: false
      })
      return false;
    }
    if (app._isTextEmpty(this.data.form.card_type)) {
      wx.showModal({
        content: '请选择银行卡类型',
        showCancel: false
      })
      return false;
    }
    if (app._isTextEmpty(this.data.form.identity)) {
      wx.showModal({
        content: '请输入证件号',
        showCancel: false
      })
      return false;
    }
    if (app._isTextEmpty(this.data.form.mobile)) {
      wx.showModal({
        content: '请输入银行卡预留手机号',
        showCancel: false
      })
      return false;
    }
    if (this.data.form.card_type == 2) {
      // 信用卡多验证两个 cvv2 valid_day
      if (app._isTextEmpty(this.data.form.cvv2)) {
        wx.showModal({
          content: '请输入卡背面末三位',
          showCancel: false
        })
        return false;
      }
      if (app._isTextEmpty(this.data.form.valid_day)) {
        wx.showModal({
          content: '请选择月份/年份',
          showCancel: false
        })
        return false;
      }
    }

    return true;
  },
  //获取图片验证码
  getimgStatus() {
    let urlStr = app.getNetAddresss("plugin.silver-point-pay.frontend.union-card.captcha-status");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            imgcodeShow: true
          })
          this.getimgdata();
        } else {
          this.setData({
            imgcodeShow: false
          })
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取图片验证码
  getimgdata() {
    let urlStr = app.getNetAddresss("home-page.getCaptcha");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.data.captcha) {
          this.setData({
            imgcode: res.data.captcha.img
          })
          console.log(this.data.imgcode, 'dddddddddd')
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 验证发送短信验证码
  checkCodeMethod() {
    if (this.imgcodeShow) {
      // 如果开启了图形验证码
      this.checkCaptcha();
    } else {
      this.firstNext();
    }
  },
  checkCaptcha() {
    let urlStr = app.getNetAddresss("plugin.silver-point-pay.frontend.union-card.captcha");
    app._getNetWork({
      url: urlStr,
      data: {
        code: this.data.sms
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result) {
          this.firstNext();
        } else {
          this.getimgdata();
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  firstNext() {
    let json = {
      card: this.data.form.card,
      bank: this.data.form.bank,
      name: this.data.form.name,
      identity: this.data.form.identity,
      mobile: this.data.form.mobile,
      card_type: this.data.form.card_type
    };
    if (this.data.form.card_type == 2) {
      json.valid_day = this.data.form.valid_day;
      json.cvv2 = this.data.form.cvv2;
    }
    let urlStr = app.getNetAddresss("plugin.silver-point-pay.frontend.union-card.bind-card");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            token: res.data.token,
            pay_union_card_id: res.data.pay_union_card_id,
          })
          this.sendCodeAnimation();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  sendCodeAnimation() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.data.count = TIME_COUNT;
      // Toast.success("验证码已发送");
      wx.showToast({
        title: '验证码已发送',
        icon: 'success',
        duration: 1000
      })
      this.data.timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          this.data.count--;
          this.setData({
            btnTag: true,
            btnText: this.data.count + "s"
          })
        } else {
          this.setData({
            btnTag: false,
            btnText: '获取验证码'
          })
          clearInterval(this.data.timer);
          this.data.timer = null;
        }
      }, 1000);
    }
  },
  confirmBtn() {
    if (app._isTextEmpty(this.smsMobile)) {
      wx.showModal({
        content: '请输入短信验证码',
        showCancel: false
      })
      return;
    }
    this.checkPhoneCode();
  },
  checkPhoneCode() {
    let json = {
      code: this.data.smsMobile,
      token: this.data.token,
      pay_union_card_id: this.data.pay_union_card_id
    };
    let urlStr = app.getNetAddresss("plugin.silver-point-pay.frontend.union-card.sms");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          if (this.data.order_pay_id) {
            // this.$dialog.alert({ message: res.msg }).then(()=>{
            //   this.$router.push(this.fun.getUrl("SilverPay",{order_pay_id:this.order_pay_id},{money:this.$route.query.money,sn:this.$route.query.sn}));
            // });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1000,
              success: function (ress) {
                setTimeout(() => {
                  wx.navigateTo({
                    url: '/packageF/silverPointPay/SilverBankList/SilverBankList',
                  })
                }, 1000);
              }
            })
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})